<?php
namespace app\base\controller;

use app\Base\model\Menu;
use CMaker\Component;
use RedisClient\RedisClient;
use think\facade\Request;
use think\Db;


class Index extends Common
{

    public function index()
    {

        $where[] = ['isshow','=','1'];
        //不是超管则只显示已拥有的菜单
        if(!in_array(1 ,explode(',',session('role.id')))){
            $where[] = ['id','in',session('role.auth_ids')];
        }

        //showSql();
        $config['treeData'] = Menu::where($where)->select()->toArray();

        $config['field'] = 'id,pid,name';

        $menu = Component::get_tree_array($config ,true ,true);

        $this->assign('menu',$menu);
        return $this->fetch();

    }
    /*
    public function main(){
        if(Request::isGet()){
            return $this->fetch();
        }

        if(Request::isPost()){
            $post = input('post.');
            $selectDate = $post['selectDate'];
            $count_with_service_money = _config('count_with_service_money');
            if($count_with_service_money == 1){
                $type_1_sql = ' IFNULL(sum(money),0) + IFNULL(sum(service_money) ,0)'; //支出
                $type_2_sql = ' IFNULL(sum(money),0) - IFNULL(sum(service_money) ,0)'; //收入
            }else{
                $type_1_sql = 'sum(money)'; //支出
                $type_2_sql = 'sum(money)'; //收入
            }

            if(session('manager.id') != 1){
                $tpwhere = [ ['manager_id' ,'=' ,session('manager.id')] ];
                $sqlwhere = ' AND manager_id = '.session('manager.id');
            }else{
                $tpwhere = [ ];
                $sqlwhere = '';
            }
            switch ($post['chartName']){
                case 'firstCharts':

                    $data['title'] = $selectDate .'财务收支统计报表';
                    //总支出
                    $res = Db::name('trading')->field($type_1_sql .'as counted')->where('type_id' ,'=',1)->where($tpwhere)->select();
                    $data['total_out'] = $res[0]['counted'];
                    //总收入
                    $res = Db::name('trading')->field($type_2_sql.'as counted')->where('type_id' ,'=',2)->where($tpwhere)->select();
                    $data['total_in'] = $res[0]['counted'];
                    $data['subtitle'] = '总收入：'.$data['total_in'].' ；总支出：'.$data['total_out'];

                    if(strpos($selectDate,'~')){
                        $arr = explode('~',$selectDate);
                        $arr[0] = trim($arr[0]);
                        $arr[1] = trim($arr[1]);

                        $sql = 'select DATE_FORMAT(add_time,\'%Y-%m-%d\') as tt,
(select '.$type_1_sql.' from jy_trading where DATE_FORMAT(add_time,\'%Y-%m-%d\') = tt AND type_id = 1 '.$sqlwhere.') as total_out,
(select '.$type_2_sql.' from jy_trading where DATE_FORMAT(add_time,\'%Y-%m-%d\') = tt AND type_id = 2 '.$sqlwhere.') as total_in
FROM jy_trading where DATE_FORMAT(add_time,\'%Y-%m\') >=  \''.$arr[0].'\' and DATE_FORMAT(add_time,\'%Y-%m\') <=  \''.$arr[1].'\' GROUP BY tt ';
                    }
                    else
                    {
                        $sql = 'select DATE_FORMAT(add_time,\'%Y-%m-%d\') as tt,
(select '.$type_1_sql.' from jy_trading where DATE_FORMAT(add_time,\'%Y-%m-%d\') = tt AND type_id = 1 '.$sqlwhere.') as total_out,
(select '.$type_2_sql.' from jy_trading where DATE_FORMAT(add_time,\'%Y-%m-%d\') = tt AND type_id = 2 '.$sqlwhere.') as total_in 
FROM jy_trading where INSTR(add_time,\''.$selectDate.'\') GROUP BY tt ';
                    }


                    $res = Db::query($sql);
                    if(count($res)){
                        foreach($res as $k => $v){
                            $data['days'][$k] = $v['tt'];
                            $data['in_money'][$k] = $v['total_in'];
                            $data['out_money'][$k] = $v['total_out'];
                        }
                    }else{
                        $data['in'][0] =  $data['out'][0] = $data['days'][0] = null;
                    }


                    break;
                case "secendCharts";
                    $data['title'] = $selectDate .'财务净收益报表';
                    //净收益
                    $in_counted = Db::name('trading')->field($type_1_sql .'as total')->where('type_id' ,'=',2)->where($tpwhere)->select();
                    $out_counted = Db::name('trading')->field($type_1_sql .'as total')->where('type_id' ,'=',1)->where($tpwhere)->select();


                    $data['subtitle'] = '总净收益：'. bcsub($in_counted[0]['total'] ,$out_counted[0]['total'] ,2).' ';

                    if(strpos($selectDate,'~')){
                        $arr = explode('~',$selectDate);
                        $arr[0] = trim($arr[0]);
                        $arr[1] = trim($arr[1]);

                        $sql = 'select DATE_FORMAT(add_time,\'%Y-%m-%d\') as tt,
(select '.$type_2_sql.' from jy_trading where DATE_FORMAT(add_time,\'%Y-%m-%d\') = tt AND type_id = 2 '.$sqlwhere.') - (select '.$type_1_sql.' from jy_trading where DATE_FORMAT(add_time,\'%Y-%m-%d\') = tt AND type_id = 1 '.$sqlwhere.') as total_in
FROM jy_trading where DATE_FORMAT(add_time,\'%Y-%m\') >=  \''.$arr[0].'\' and DATE_FORMAT(add_time,\'%Y-%m\') <=  \''.$arr[1].'\' GROUP BY tt ';

                    }else{
                        $sql = 'select DATE_FORMAT(add_time,\'%Y-%m-%d\') as tt,
((select '.$type_2_sql.' from jy_trading where DATE_FORMAT(add_time,\'%Y-%m-%d\') = tt AND type_id = 2 '.$sqlwhere.') - (select '.$type_1_sql.' from jy_trading where DATE_FORMAT(add_time,\'%Y-%m-%d\') = tt AND type_id = 1 '.$sqlwhere.')) as total_in 
FROM jy_trading where INSTR(add_time,\''.$selectDate.'\') GROUP BY tt ';
                    }


                    $res = Db::query($sql);
                    if(count($res)){
                        foreach($res as $k => $v){
                            $data['days'][$k] = $v['tt'];
                            $data['in_money'][$k] = $v['total_in'];
                        }
                    }else{
                        $data['in_money'][0] = $data['days'][0] = null;
                    }

                    break;
            }

            p($data);
            return $data;
        }

    }
    */
    public function main(){
        if(Request::isGet()){
            return $this->fetch();
        }
        if(Request::isPost()){
            $post = input('post.');
            $selectDate = $post['selectDate'];
            $redisClient = RedisClient::init(['db' => 2]);
            //p($selectDate);
            $count_with_service_money = _config('count_with_service_money');

            if(strpos($selectDate ,'~')){
                $all_month = ['01','02','03','04','05','06','07','08','09','10','11','12'];
                $arr = explode('~',$selectDate);
                //p($arr[0]);
                //p($arr[1]);

                $s = explode('-',$arr[0]);
                $s_year = trim($s[0]) ;
                $s_month = trim($s[1]);
                $e= explode('-',$arr[1]);
                $e_year = trim($e[0]);
                $e_month = trim($e[1]);
                if($s_year == $e_year){
                    if($s_month == $e_month){
                        $s_e_keys = [$s_month];
                    }else{
                        $s_e_keys = array_slice( $all_month , array_keys($all_month ,$s_month)[0] ,($e_month - $s_month)  + 1);
                    }

                    $allkeys = [];
                    foreach($s_e_keys as $k => $v){

                        $arg = $redisClient->keys($s_year.'-'.$v.':*');
                        asort($arg);
                        sort($arg);
                        array_push($allkeys , $arg);
                    }

                }else{
                    $s_key_month = array_slice( $all_month , array_keys($all_month ,$s_month)[0] ,count($all_month));
                    $allkeys = [];
                    foreach($s_key_month as $k => $v){
                        $arg = $redisClient->keys($s_year.'-'.$v.':*');
                        asort($arg);
                        sort($arg);
                        array_push($allkeys , $arg);
                    }

                    $e_key_month = array_slice( $all_month , 0 ,array_keys($all_month ,$e_month)[0] + 1);

                    foreach($e_key_month as $k => $v){
                        $arg = $redisClient->keys($e_year.'-'.$v.':*');
                        asort($arg);
                        sort($arg);
                        array_push($allkeys , $arg);
                    }

                }
                $keys = [];
                foreach ($allkeys as $k => $v){
                    foreach ($v as $sk => $sv){
                        array_push($keys ,$sv);
                    }
                }

            }else{
                $keys = $redisClient->keys($selectDate.':*');
                asort($keys);
                sort($keys);
            }



            $group_days = [];
            foreach($keys as $k => $v){
                $arg = explode(':',$v);
                $days = $arg[0].'-'.$arg[1];
                $group_days[$days][$k] = $v;
            }
            $data = [];
            $data['title'] = $selectDate .'财务收支统计报表';
            $data['title2'] = $selectDate .'财务净收益报表';
            $data['total_out'] = $data['total_in'] =  $data['total_in_come'] = 0.00;

            $i = 0;
            foreach($group_days as $k => $v){

                $data['days'][$i] = $k;
                $data['in_money'][$i] = $data['out_money'][$i] = $data['in_come'][$i] = 0.00;

                foreach($v as $sk => $sv){
                    $res = json_decode( RedisClient::init(['db' => 2])->get($sv) ,true);

                    if(session('manager.id') != 1 && $res['manager_id'] == session('manager.id') )
                    {
                        switch ($res['type_id']){
                            case '2':// Unicode编码 \u6536\u5165 收入
                                $res['money'] = ($count_with_service_money == 1) ? bcsub($res['money'] ,$res['service_money'] ,2) : $res['money'];
                                $data['total_in'] = bcadd($data['total_in'] , $res['money'] ,2 );
                                $data['in_money'][$i] = bcadd( $data['in_money'][$i] , $res['money'] ,2);
                                break;
                            case '1' : // Unicode编码 \u652f\u51fa 支出
                                $res['money'] = ($count_with_service_money == 1) ? bcadd($res['money'] ,$res['service_money'] ,2) : $res['money'];
                                $data['total_out'] = bcadd($data['total_out'] , $res['money'] ,2 );
                                $data['out_money'][$i] = bcadd( $data['out_money'][$i] , $res['money'] ,2);
                                break;
                        }


                        $data['in_come'][$i] = bcsub($data['in_money'][$i] ,$data['out_money'][$i] ,2);

                    }
                    elseif(session('manager.id') == 1)
                    {

                        switch ($res['type_id']){
                            case '2':// Unicode编码 \u6536\u5165 收入
                                $res['money'] = ($count_with_service_money == 1) ? bcsub($res['money'] ,$res['service_money'] ,2) : $res['money'];
                                $data['total_in'] = bcadd($data['total_in'] , $res['money'] ,2 );
                                $data['in_money'][$i] = bcadd( $data['in_money'][$i] , $res['money'] ,2);
                                break;
                            case '1' : // Unicode编码 \u652f\u51fa 支出
                                $res['money'] = ($count_with_service_money == 1) ? bcadd($res['money'] ,$res['service_money'] ,2) : $res['money'];
                                $data['total_out'] = bcadd($data['total_out'] , $res['money'] ,2 );
                                $data['out_money'][$i] = bcadd( $data['out_money'][$i] , $res['money'] ,2);
                                break;
                        }

                        $data['in_come'][$i] = bcsub($data['in_money'][$i] ,$data['out_money'][$i] ,2);
                    }
                    else
                    {
                        continue;
                    }


                }



                $i++;
            }


            foreach($data['in_come'] as $k => $v){
                $data['total_in_come'] = bcadd($data['total_in_come'] , $v , 2);
            }
            $data['subtitle2'] = '总净收益'. $data['total_in_come'] ;


            $data['subtitle'] = '总收入：'.$data['total_in'].' ；总支出：'.$data['total_out'];
            //p($data);

            return $data;

        }

    }
}
